package cn.ciis.server;

import cn.ciis.basic.kit.ByteKit;
import cn.hutool.core.util.StrUtil;
import org.tio.core.intf.Packet;

import java.math.BigInteger;

/** @author 冉志林 */
public class CiisPacket extends Packet {

  private static final long serialVersionUID = -172060606924066412L;
  /** 消息头的长度 */
  public static final byte HEADER[]= new byte[]{126,90};//消息头
  public static final byte END[] = new byte[]{13,13};//消息尾

  private byte[] type = new byte[1];//帧类型
  private byte[] enterprise = new byte[2];//企业号
  private byte[] device = new byte[2];//设备
  private byte[] cmd = new byte[2];//执行命令
  private byte[] bodyLength = new byte[2];//内容长度

  private int neededLength=0;

  public static final String CHARSET = "UTF-8";
  private byte[] body;//内容
  private String dataBody;//
  private int bodyLen=0;

  public byte[] getBody() {
    return body;
  }
  public void setBody(byte[] body) {
    this.body = body;
  }

  public String getDataBody() {
    return dataBody;
  }
  public void setDataBody(String dataBody) {
    this.dataBody = dataBody;
  }

  public byte[] getType() {
    return type;
  }

  public void setType(byte[] type) {
    this.type = type;
  }

  public byte[] getCmd() {
    return cmd;
  }

  public void setCmd(byte[] cmd) {
    this.cmd = cmd;
  }

  public byte[] getEnterprise() {
    return enterprise;
  }

  public void setEnterprise(byte[] enterprise) {
    this.enterprise = enterprise;
  }

  public byte[] getDevice() {
    return device;
  }

  public void setDevice(byte[] device) {
    this.device = device;
  }

  public byte[] getBodyLength() {
    return bodyLength;
  }

  public void setBodyLength(byte[] bodyLength) {
    this.bodyLength = bodyLength;

  }

  public int getBodyLen(){
    if(this.bodyLen <=0){
      this.bodyLen = new BigInteger(ByteKit.bytesToHexString(bodyLength), 16).intValue();
      this.body = new byte[this.bodyLen];
    }
    return this.bodyLen;

  }


  /**
   * 获取需要Tio读取消息的长度
   * @return
   */
  public int getNeededLength(){
    this.neededLength = CiisPacket.HEADER.length+this.type.length+this.enterprise.length+this.device.length+this.cmd.length
              + getBodyLen() + CiisPacket.END.length;

    return this.neededLength;
  }

  /**
   * 封装发送消息Buffer需要的长度
   * @param bodyLength
   * @return
   */
  public int getSendLength(int bodyLength){

    return CiisPacket.HEADER.length+this.type.length+this.enterprise.length+this.device.length+this.cmd.length+this.bodyLength.length
            + bodyLength +CiisPacket.END.length;
  }

  /**
   * 获取解码后的完整数据
   * @return
   */
  public String getDec(){

    String result = StrUtil.format("{}{}{}{}{}{}{}{}", ByteKit.bytesToHexString(CiisPacket.HEADER)
            ,ByteKit.bytesToHexString(this.type),ByteKit.bytesToHexString(this.enterprise),ByteKit.bytesToHexString(this.device),
            ByteKit.bytesToHexString(this.cmd),ByteKit.bytesToHexString(this.bodyLength),ByteKit.bytesToHexString(this.body),ByteKit.bytesToHexString(CiisPacket.END));
    return  result;
  }
}
